<?php
// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Model;

class UserSignLog extends Model
{
    protected function getOptions(): array
    {
        return [
            'autoWriteTimestamp' => true,
            'createTime'         => 'create_time',
            'updateTime'         => false,
            'jsonAssoc'          => true,
        ];
    }

    /**
     * 关联用户
     */
    public function user()
    {
        return $this->belongsTo('User', 'user_id');
    }

    /**
     * 获取用户签到记录
     * @param int $userId 用户ID
     * @param string $startDate 开始日期，格式：Y-m-d
     * @param string $endDate 结束日期，格式：Y-m-d
     * @return array
     */
    public static function getUserSignLogs($userId, $startDate = '', $endDate = '')
    {
        $query = self::where('user_id', $userId);

        if (!empty($startDate)) {
            $query->where('sign_date', '>=', $startDate);
        }

        if (!empty($endDate)) {
            $query->where('sign_date', '<=', $endDate);
        }

        return $query->order('sign_date', 'desc')
            ->select()
            ->toArray();
    }

    /**
     * 获取用户当月签到记录
     * @param int $userId 用户ID
     * @param string $month 月份，格式：Y-m，默认当月
     * @return array
     */
    public static function getUserMonthSignLogs($userId, $month = '')
    {
        if (empty($month)) {
            $month = date('Y-m');
        }

        $startDate = $month . '-01';
        $endDate   = date('Y-m-t', strtotime($startDate));

        return self::getUserSignLogs($userId, $startDate, $endDate);
    }

    /**
     * 检查用户今天是否已签到
     * @param int $userId 用户ID
     * @return bool
     */
    public static function checkTodaySigned($userId)
    {
        $today = date('Y-m-d');

        return self::where('user_id', $userId)
            ->where('sign_date', $today)
            ->count() > 0;
    }

    /**
     * 获取用户连续签到天数
     * @param int $userId 用户ID
     * @return int
     */
    public static function getUserContinuousDays($userId)
    {
        $today = date('Y-m-d');

        // 获取最近一次签到记录
        $lastSign = self::where('user_id', $userId)
            ->order('sign_date', 'desc')
            ->find();

        if (!$lastSign) {
            return 0;
        }

        // 如果最近一次签到是今天，返回连续天数
        if ($lastSign->sign_date == $today) {
            return $lastSign->continuous_days;
        }

        // 如果最近一次签到是昨天，返回连续天数
        $yesterday = date('Y-m-d', strtotime('-1 day'));
        if ($lastSign->sign_date == $yesterday) {
            return $lastSign->continuous_days;
        }

        // 否则连续签到中断，返回0
        return 0;
    }

    /**
     * 获取签到规则
     * @return array
     */
    public static function getSignRules()
    {
        return [
            ['day' => 1, 'points' => 5],
            ['day' => 2, 'points' => 6],
            ['day' => 3, 'points' => 7],
            ['day' => 4, 'points' => 8],
            ['day' => 5, 'points' => 9],
            ['day' => 6, 'points' => 10],
            ['day' => 7, 'points' => 15],
        ];
    }

    /**
     * 获取用户今天签到积分
     * @param int $userId 用户ID
     * @return int
     */
    public static function getTodayPoints($userId)
    {
        $today   = date('Y-m-d');
        $signLog = self::where('user_id', $userId)
            ->where('sign_date', $today)
            ->find();
        return $signLog->points ?? 0;
    }
}